package com.mcjtf.prefix.model;

import com.mcjtf.prefix.PrefixManager;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Data
public class User {
    private String name;
    private UUID uuid;
    private String group;
    private String displayName;
    private int usePrefix;
    private int useSuffix;
    private List<String> prefixes;
    private List<String> suffixes;
    private List<String> groupPrefix;
    private List<String> groupSuffix;

    public User(String name) {
        this.name = name;
        this.prefixes = new ArrayList<>();
        this.suffixes = new ArrayList<>();
        this.usePrefix = 0;
        this.useSuffix = 0;
        this.group = null;
        this.displayName = name;
    }

    public void setUsePrefix(int id) {
        this.usePrefix = id >= prefixes.size() ? 0 : id;
        refresh();
    }

    public void setUseSuffix(int id) {
        this.useSuffix = id >= suffixes.size() ? 0 : id;
        refresh();
    }

    public void refresh() {
        StringBuilder builder = new StringBuilder(name);
        String prefix = prefixes.get(getUsePrefix());
        String suffix = suffixes.get(getUseSuffix());
        if (prefix != null) {
            builder.insert(0, prefix);
        }
        if (suffix != null) {
            builder.append(suffix);
        }
        setDisplayName(builder.toString());
    }

    public String getPrefix() {
        return PrefixManager.getInstance().msg(prefixes.get(getUsePrefix()));
    }

    public String getSuffix() {
        return PrefixManager.getInstance().msg(suffixes.get(getUseSuffix()));
    }

    public boolean addPrefix(String prefix) {
        if (this.prefixes.contains(prefix)) {
            return false;
        }
        return this.prefixes.add(prefix);
    }

    public boolean addSuffix(String suffix) {
        if (this.suffixes.contains(suffix)) {
            return false;
        }
        return this.suffixes.add(suffix);
    }

    public boolean removePrefix(int id) {
        return this.prefixes.remove(id) != null;
    }

    public boolean removeSuffix(int id) {
        return this.suffixes.remove(id) != null;
    }
}
